package com.newland.mesdk.demo.interfaceimpl;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import com.newland.mtype.ModuleType;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.keyboard.KeyBoard;
import com.newland.mtype.module.common.keyboard.KeyBoardReadingEvent;
import com.newland.mtype.module.common.lcd.DispType;
import com.newland.mesdk.demo.moduleinterface.KeyBoardInterface;
import com.newland.mesdk.demo.util.ModuleBase;

/**
 * Created by YJF 密码键盘模块接口实现
 */
public class KeyBoardInterfaceImpl extends ModuleBase implements KeyBoardInterface {
	private KeyBoard keyBoard;

	public KeyBoardInterfaceImpl() {
		keyBoard = (KeyBoard) factory.getModule(ModuleType.COMMON_KEYBOARD);
	}

	// 0撤消上次读取操作.
	@Override
	public void cancelLastReading() {
		keyBoard.cancelLastReading();
	}

	// 1从键盘缓冲区立刻读取首个键值
	@Override
	public byte read() {
		try {
			return keyBoard.read();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	// 2在限定时间内,从键盘缓冲区读取键值
	@Override
	public byte read(long readTimeout, TimeUnit timeunit) {
		byte keyBoardRead = keyBoard.read(readTimeout, timeunit);
		return keyBoardRead;
	}

	// 3开启一个标准金额读取流程
	@Override
	public void readAmt(DispType dispType, String title, String content, int minLength, int maxLength, long timeout, TimeUnit timeunit,
			DeviceEventListener<KeyBoardReadingEvent<BigDecimal>> listener) {
		keyBoard.readAmt(dispType, title, content, maxLength, maxLength, timeout, timeunit, listener);
	}

	// 4开启一个标准数字读取流程
	@Override
	public void readNumber(DispType dispType, String title, String content, int minLength, int maxLength, long timeout, TimeUnit timeunit,
			DeviceEventListener<KeyBoardReadingEvent<BigDecimal>> listener) {
		keyBoard.readNumber(dispType, title, content, minLength, maxLength, timeout, timeunit, listener);
	}

	// 开启一个标准字密码读取流程
	@Override
	public void readPwd(DispType dispType, String title, String content, int minLength, int maxLength, long timeout, TimeUnit timeunit,
			DeviceEventListener<KeyBoardReadingEvent<String>> listener) {
		keyBoard.readPwd(dispType, title, content, minLength, maxLength, timeout, timeunit, listener);
	}

	// 开启一个标准字符串读取流程
	@Override
	public void readString(DispType dispType, String title, String content, int minLength, int maxLength, long timeout, TimeUnit timeunit,
			DeviceEventListener<KeyBoardReadingEvent<String>> listener) {
		keyBoard.readString(dispType, title, content, minLength, maxLength, timeout, timeunit, listener);
	}
}
